package com.tedu.element.item;

import com.tedu.element.ElementObj;
import com.tedu.element.map.TileType;
import com.tedu.element.player.Player;
import com.tedu.manager.GameLoad;
import com.tedu.show.GameConstants;

import java.awt.*;

import javax.swing.*;

/**
 * @说明 道具类
 */
public class Item extends ElementObj {
    private ItemType type;
    private final int itemSize = GameConstants.TILE_SIZE;
    private int currentFrame = 0;   // 当前帧索引
    private long lastFrameTime;     // 上次帧切换时间

    public Item() {
    }

    // str = "gridX,gridY,type"
    @Override
    public ElementObj createElement(String str) {
        String[] split = str.split(",");

        // 设置道具网格坐标
        this.setGridX(Integer.parseInt(split[0]));
        this.setGridY(Integer.parseInt(split[1]));

        // 设置道具大小
        this.setW(itemSize);
        this.setH(itemSize);

        // 设置道具类型
        // this.type = ItemType.randomType();
        this.type = ItemType.valueOf(split[2]);

        // 设置道具图片
        loadSpriteImage();

        // 初始化动画计时器
        this.lastFrameTime = System.currentTimeMillis();

        return this;
    }

    /**
     * 根据道具类型加载对应图片
     */
    private void loadSpriteImage() {
        String groupName = "item";
        String stateName = this.type.toString().toLowerCase();

        // 获取精灵图
        ImageIcon spriteIcon = GameLoad.groupImgMap.get(groupName).get(stateName);
        if (spriteIcon == null) {
            System.err.println("道具精灵图未找到: " + stateName);
            return;
        }
        this.setIcon(spriteIcon);
    }

    @Override
    public void showElement(Graphics g) {
        if (getIcon() == null) {
            return;
        }

        // 更新动画帧
        updateAnimation();

        // 计算当前帧在精灵图中的位置
        // 精灵图单帧宽度
        int spriteWidth = GameConstants.ITEM_SPRITE_WIDTH;
        int srcX = currentFrame * spriteWidth;
        int srcY = 0;
        // 计算绘制位置
        int drawX = getX();
        int drawY = getY();

        // 绘制当前帧
        // 精灵图单帧高度
        int spriteHeight = GameConstants.ITEM_SPRITE_HEIGHT;
        g.drawImage(getIcon().getImage(),
                drawX, drawY,
                drawX + itemSize,
                drawY + itemSize,
                srcX, srcY,
                srcX + spriteWidth,
                srcY + spriteHeight,
                null);

        if (GameConstants.DEBUG_MODE) {
            g.setColor(new Color(255, 0, 0, 100));
            Rectangle rect = getRectangle();
            g.fillRect(rect.x, rect.y, rect.width, rect.height);
        }
    }

    /**
     * 更新动画帧
     */
    private void updateAnimation() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastFrameTime > GameConstants.ITEM_ANIMATION_SPEED) {
            currentFrame = (currentFrame + 1) % GameConstants.ITEM_FRAME_COUNT;
            lastFrameTime = currentTime;
        }
    }

    public void applyEffect(Player player) {
        switch (type) {
            case POWER:
                player.increaseBombPower();
                break;
            case COUNT:
                player.increaseBombCount();
                break;
            case SPEED:
                player.increaseSpeed();
                break;
        }
    }
}